package xuhaijia.lanou3g.autohome.forum.normalforum;
//                .-~~~~~~~~~-._       _.-~~~~~~~~~-.
//            __.'              ~.   .~              `.__
//          .'//                  \./                   \\`.
//        .'//            为什么坚持  想一想当初             \\`.
//      .'// .-~"""""""~~~~-._     |     _,-~~~~"""""""~-.  \\`.
//    .'//.-"                 `-.  |  .-'                 "-. \\`.
//  .'//______.============-..   \ | /   ..-============._______\\`.
//.'//____________________________\|/_____________________________\\`.
// 
//                             

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;

import xuhaijia.lanou3g.autohome.R;
import xuhaijia.lanou3g.autohome.custom.SectionedBaseAdapter;
import xuhaijia.lanou3g.autohome.base.NetTool;

/**
 * Created by 7 on 16/8/19.
 */
public class CarTypeAdapter extends SectionedBaseAdapter {
    private Context context;
    private String[] letter;
    private ArrayList<HashMap<String, String>> type;
    private NetTool mNetTool = new NetTool();
    private ArrayList<ArrayList<String>> name;

    public CarTypeAdapter(Context context) {
        this.context = context;
    }

    public void setType(ArrayList<HashMap<String, String>> type) {
        this.type = type;
        notifyDataSetChanged();
    }

    public void setName(ArrayList<ArrayList<String>> name) {
        this.name = name;
        notifyDataSetChanged();
    }

    public void setLetter(String[] letter) {
        this.letter = letter;
        notifyDataSetChanged();
    }

    @Override
    public Object getItem(int section, int position) {
        return name.get(section).get(position);
    }

    @Override
    public long getItemId(int section, int position) {
        return position;
    }

    @Override
    public int getSectionCount() {   // 分节数
        return letter.length;
    }

    @Override
    public int getCountForSection(int section) {
        if (name.get(section) == null) {
            return 0;
        }
        return name.get(section).size();
    }

    @Override
    public View getItemView(int section, int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;

        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_section_lv, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        String key = name.get(section).get(position);
        mNetTool.getImage(type.get(section).get(key), viewHolder.iv);
        viewHolder.tv.setText(key);
        return convertView;
    }

    @Override
    public View getSectionHeaderView(int section, View convertView, ViewGroup parent) {
        ViewHolderForHeader viewHolderForHeader = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_section_header, null);

            viewHolderForHeader = new ViewHolderForHeader(convertView);
            convertView.setTag(viewHolderForHeader);
        } else {
            viewHolderForHeader = (ViewHolderForHeader) convertView.getTag();
        }
        viewHolderForHeader.letterTv.setText(letter[section]);
        convertView.setClickable(false);
        return convertView;
    }

    public class ViewHolder {
        TextView tv;
        ImageView iv;

        public ViewHolder(View view) {
            tv = (TextView) view.findViewById(R.id.sectionlv_tv);
            iv = (ImageView) view.findViewById(R.id.sectionlv_iv);
        }
    }

    private class ViewHolderForHeader {
        TextView letterTv;

        public ViewHolderForHeader(View view) {
            letterTv = (TextView) view.findViewById(R.id.item_section_letter);
        }
    }
}
